import 'package:flutter/material.dart';
import 'package:situation/app/theme/app_theme.dart';
import 'package:situation/app/theme/gaps.dart';
import 'package:situation/data/store/config_store.dart';
import 'package:situation/domain/intell/intell_history_dto.dart';
import 'package:situation/shared/util/util_mk.dart';
import 'package:situation/widgets/PlayVideo.dart';
import 'package:situation/widgets/mk_load_image.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class AppIntell extends StatefulWidget {
  final IntellHistoryDto intell;

  const AppIntell({super.key, required this.intell});

  @override
  State<AppIntell> createState() => _AppIntellState();
}

class _AppIntellState extends State<AppIntell> {
  Widget _buildLeft() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Text(
          "${UtilMk.formatDateTimeGetMonth(widget.intell.createTime)} / ${UtilMk.formatDateTimeGetDay(widget.intell.createTime)}",
          style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
        ),
        Gaps.vGap4,
        Text(
          "${UtilMk.formatDateTimeGetYear(widget.intell.createTime)}",
          style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
        ),
        Gaps.vGap4,
        Text(
          "${UtilMk.formatDateTimeGetHis(widget.intell.createTime)}",
          style: TextStyle(fontSize: 12),
        ),
        Gaps.vGap4,
        Text(
          "# ${widget.intell.typeStr}",
          style: TextStyle(fontSize: 12, color: Colors.redAccent),
        ),
      ],
    );
  }

  Widget _buildRight() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "${widget.intell.position}",
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
        ),
        Gaps.vGap4,
        Text(
          "${widget.intell.detail ?? ''}",
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 12),
        ),
        Gaps.vGap8,
        widget.intell.annexs.isEmpty
            ? Container()
            : Container(
              height: (widget.intell.annexs.length / 3).ceil() * 160,
              child: GridView.count(
                physics: NeverScrollableScrollPhysics(),
                crossAxisCount: 3,
                mainAxisSpacing: 8,
                crossAxisSpacing: 8,
                // 每行三个项
                children: List.generate(
                  widget.intell.annexs.length, // 生成9个项
                  (i) => ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child:
                        widget.intell.annexs[i].type == 1
                            ? GestureDetector(
                              onTap: () {
                                TDImageViewer.showImageViewer(
                                  context: context,
                                  images: [
                                    ConfigStore.to.baseFileUrl +
                                        widget.intell.annexs[i].path,
                                  ],
                                );
                              },
                              child: MkLoadImage(
                                ConfigStore.to.baseFileUrl +
                                    widget.intell.annexs[i].path,
                              ),
                            )
                            : GestureDetector(
                              onTap: () {
                                UtilMk.openBottomSheet(
                                  480,
                                  "视频",
                                  Container(
                                    height: 400,
                                    clipBehavior: Clip.hardEdge,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(
                                        AppTheme.iDefaultRadius * 2,
                                      ),
                                    ),
                                    child: PlayVideo(
                                      url:
                                          ConfigStore.to.baseFileUrl +
                                          widget.intell.annexs[i].path,
                                    ),
                                  ),
                                );
                              },
                              child: Stack(
                                alignment: Alignment.center,
                                children: [
                                  MkLoadImage(
                                    ConfigStore.to.baseFileUrl +
                                        widget.intell.annexs[i].coverImg,
                                  ),
                                  Icon(
                                    Icons.play_circle,
                                    color: Colors.blueAccent,
                                  ),
                                ],
                              ),
                            ),
                  ),
                ),
              ),
            ),
        Gaps.vGap8,
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Text(
                "${widget.intell.position}",
                style: TextStyle(fontSize: 12, color: Colors.blue),
              ),
            ),
            TDTag(
              '${widget.intell.statusStr}',

              theme: TDTagTheme.danger,
              isOutline: true,
              size: TDTagSize.small,
            ),
          ],
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(
        vertical: AppTheme.iDefaultPadding / 2,
        horizontal: AppTheme.iDefaultPadding,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.2),
            spreadRadius: 2,
            blurRadius: 3,
            offset: Offset(0, 3), // changes position of shadow
          ),
        ],
      ),
      padding: EdgeInsets.all(AppTheme.iDefaultPadding),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [_buildLeft(), Gaps.hGap24, Expanded(child: _buildRight())],
      ),
    );
  }
}
